package com.jiazhong.front.scheduled;

import com.jiazhong.commons.Mapper.ShoppingCartMapper;
import com.jiazhong.commons.bean.ShoppingCart;
import com.jiazhong.front.service.impl.ShoppingCartServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Component
public class GwcScheduled {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Scheduled(cron = "0 57 15 * * *")
    public void backups(){
        // 1. 从redis中获取到所有购物车信息
        HashOperations<String, Object, Object> forHash = stringRedisTemplate.opsForHash();
        ListOperations<String, String> forList = stringRedisTemplate.opsForList();
        Map<Object, Object> carts = forHash.entries(ShoppingCartServiceImpl.GWC_KEY);
        List<ShoppingCart> addList = new ArrayList<>();
        List<ShoppingCart> updateList = new ArrayList<>();
        List<ShoppingCart> removeList = new ArrayList<>();
        //2.将cart中的数据组装成list
        Set<Object> userIds = carts.keySet();
        for (Object userId:userIds) {
            String listKey = forHash.get(ShoppingCartServiceImpl.GWC_KEY, userId).toString();
            List<String> cart = forList.range(listKey, 0, -1);
            int i = 0;
            for (String c:cart) {
                ShoppingCart sc = new ShoppingCart();
                int firstIndex = c.indexOf("#");
                int lastIndex = c.lastIndexOf("#");
                String bookId = c.substring(0, firstIndex);
                int number = Integer.parseInt(c.substring(firstIndex + 1, lastIndex));
                int state = Integer.parseInt(c.substring(lastIndex + 1));
                sc.setUserId(userId.toString());
                sc.setNumber(number);
                sc.setBookId(bookId);
                switch (state) {
                    case 0 -> {
                        addList.add(sc);
                        // 统一修改状态-->1
                        forList.set(listKey, i, bookId + "#" + number + "#" + 1);
                    }
                    case 2 -> {
                        updateList.add(sc);
                        // 统一修改状态-->1
                        forList.set(listKey, i, bookId + "#" + number + "#" + 1);
                    }
                    case 3 -> {
                        removeList.add(sc);
                        forList.remove(listKey,1,bookId + "#" + 666 + "#" + 3);
                        i--;
                    }
                }
                i++;
            }
        }
        // 3. 存放到数据库
        shoppingCartMapper.insert(addList);
        // 删除
        for (int i = 0; i < removeList.size(); i++) {
            shoppingCartMapper.deleteByBookId(removeList.get(i));
        }
        // 修改
        for (int i = 0; i < updateList.size(); i++) {
            shoppingCartMapper.updateNumberByBookIdAndUserId(updateList.get(i));
        }
    }
}
